﻿using Serilog;
using Serilog.Context;
using Serilog.Core;
using Serilog.Events;
using System.Diagnostics;
using System.Runtime.CompilerServices;

namespace YY.Admin.Core
{
    public class SerilogLoggerService : ILoggerService
    {
        private readonly Serilog.ILogger _logger;

        public SerilogLoggerService()
        {
            _logger = new LoggerConfiguration()
                .MinimumLevel.Debug()
                .Enrich.WithThreadId()
                .Enrich.WithMachineName()
                .WriteTo.Console(outputTemplate: "{Timestamp:yyyy-MM-dd HH:mm:ss.fff} [{Level:u3}] {Message:lj}{NewLine}{Exception}")
                .WriteTo.File("logs/app.log",
                    rollingInterval: RollingInterval.Day,
                    retainedFileCountLimit: 7,
                    outputTemplate: "{Timestamp:yyyy-MM-dd HH:mm:ss.fff} [{Level:u3}] ({ThreadId}) {Message:lj}{NewLine}{Exception}")
                .CreateLogger();
        }

        public void Information(string message) => _logger.Information(message);
        public void Warning(string message) => _logger.Warning(message);
        public void Error(string message, Exception? ex = null) => _logger.Error(ex, message);
        public void Debug(string message) => _logger.Debug(message);

        public void Performance(string operationName, TimeSpan duration)
        {
            _logger.Information("Performance: {Operation} completed in {Duration}ms",
                operationName, duration.TotalMilliseconds);
        }
    }
}
